<?php
namespace app\api\controller\zfshop;
use x\my\pay\Alipay;
use x\my\Pay;
class Uservip extends Base
{
    //升级 vip

    public function __construct() {
        parent::__construct();
        if(!$this->userinfo){
            if(is_post()){
                err('请先登录!',200);
            }
        }
    }

    //  店铺详情
    public function add(){
        $d = $this->post();
        $rule = [
            'level_id'=>'require',
            'pay_type'=>'require',
        ];
        $msg = [];
        check($rule,$msg,$d);
        if(isset($d["spassword"]) && !empty($d["spassword"])){
            $this->checkSPassword();
        }
        $model = m('UserVip','c');
        $info = $model->add($this->userinfo["id"],$d["level_id"],$d["pay_type"],$d['invitecode'],$d["user_sign"]);
        if(!$info) err($model->getError());
        //在线支付
        if($info['pay_money']){
            //充值余额
            $recharge_order_no = m('recharge', 'c')->add($this->userinfo['id'], $info['pay_money'], $d['pay_type'],'在线购买','user_vip',0,$info["order_no"]);
            if(!$recharge_order_no) err(m("recharge", "c")->getError());
            #充值信息
            $recharge_info = $this->db->query("select * from recharge where order_no=?",[$recharge_order_no],1);
            $total_fee = bcmul($recharge_info['money'],100);

            #微信 #4 统一下单
            if($d['pay_type']==0){
                $rs = [];
                #开放平台->移动应用->商户号
                if(in_array($d['partner'],[5100,5200])){
                  #七色淘品
                  $config = [
                    'appid' => c('api.wx_app_appid'),
                    'mch_id' => c('api.wx_app_mchid'),
                    'key'    => c('api.wx_app_key'),
                    'mode'   => 'optional',
                  ];
                  $pay = Pay::wechat($config);
                  $order = [
                    'body' => '在线购买',
                    'out_trade_no' => $recharge_info['order_no'],
                    'total_fee' => $total_fee,
                    'notify_url' => url('notice/wxrecharge'),
                    'trade_type' => 'APP',
                  ];
                  $rs = $pay->app($order)->getContent();
                  $rs = json_decode($rs,true);
                  $data = [
                      'appid'=>$rs['appid'],
                      'partnerid'=>$rs['partnerid'],
                      'prepayid'=>$rs['prepayid'],
                      'package' => $rs['package'],
                      'noncestr'=> $rs['noncestr'],
                      'timestamp'=>$rs['timestamp'],
                      'sign' => $rs['sign'],
                      'order_no'=>$recharge_info['order_no'],
                  ];
                  //$data['sign'] = Pay::sign($data);
                  suc($data);
                #h5 支付
                }else if(in_array($d['partner'],[5300,5400])){
                    err('h5支付 暂未开放');
                #小程序支付
                }else if(in_array($d['partner'],[5500])){
                  if(!isset($d['openid'])) err('openid 不能为空');
                  $config = [
                    #开放平台->移动应用->商户号->授权小程序
                    'miniapp_id' => c('api.wx_xcx_appid'),
                    'mch_id' => c('api.wx_pay_mchid'),
                    'key' => c('api.wx_pay_key'),
                    'mode'   => 'optional',
                  ];
                  $pay = Pay::wechat($config);
                  $order = [
                      'out_trade_no' => $recharge_info['order_no'],
                      'body'      => '在线购买',
                      'total_fee' => $total_fee,
                      'openid'    => $d['openid'],
                      'notify_url' => url('notice/wxrecharge'),
                  ];
                  $rs = $pay->miniapp($order);
                  suc($rs);
                }
            #支付宝
            }else if($d['pay_type']==1){
                err('支付宝支付,暂未开放');
            }
        //直接付
        }else{
            $pay_rs = s("UserVip", "c")->pay($info["order_no"]);
            suc();
        }
    }

}
